//
//  BEFontExtension.swift
//  BasicExtensions
//
//  Created by Fanxx on 2019/5/22.
//  Copyright © 2019 Fanxx. All rights reserved.
//

import UIKit

extension UIFont {
    public var isBold: Bool {
        return self.fontDescriptor.symbolicTraits.contains(.traitBold)
    }
    public var isItalic: Bool {
        return self.fontDescriptor.symbolicTraits.contains(.traitItalic)
    }
    /// 转为加粗
    public var asBold: UIFont {
        return self.asTraits(.traitBold)
    }
    /// 转为斜体
    public var asItalic: UIFont {
        return self.asTraits(.traitItalic)
    }
    /// 转换属性
    public func asTraits(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
        if let descriptor = self.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits)) {
            return UIFont(descriptor: descriptor, size: self.pointSize)
        }
        return self
    }
}

extension UIFont {
    ///使用family name实例化，若没有该字体则返回系统字体
    public static func `try`(name: String, size: CGFloat) -> UIFont {
        return .init(name: name, size: size) ?? .systemFont(ofSize: size)
    }
    ///苹方简体
    public static func pingfang(_ size: CGFloat) -> UIFont {
        return UIFont.try(name: "PingFangSC-Regular", size: size)
    }
    ///苹方简体
    public static func pingfang(medium size: CGFloat) -> UIFont {
        return UIFont.try(name: "PingFangSC-Medium", size: size)
    }
    ///苹方简体
    public static func pingfang(bold size: CGFloat) -> UIFont {
        return UIFont.try(name: "PingFangSC-Semibold", size: size)
    }
    ///苹方简体
    public static func pingfang(light size: CGFloat) -> UIFont {
        return UIFont.try(name: "PingFangSC-Light", size: size)
    }
    ///苹方简体
    public static func pingfang(ultralight size: CGFloat) -> UIFont {
        return UIFont.try(name: "PingFangSC-Ultralight", size: size)
    }
    ///苹方简体
    public static func pingfang(thin size: CGFloat) -> UIFont {
        return UIFont.try(name: "PingFangSC-Thin", size: size)
    }
}
